package com.xzcedu.homework;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author 徐州城
 * @version 1.0
 * TCP服务端
 */
public class Homework01Server {
    public static void main(String[] args) throws IOException {

        //新建端口为 7749
        ServerSocket serverSocket = new ServerSocket(7749);
        System.out.println("服务端 端口7749正在监听...");
        //得到socket对象
        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();//接收数据 创建字节输入流
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
//        while ((line = br.readLine()) != null) {
//            System.out.println(line);
//        }
        line = br.readLine();
        System.out.println(line);

        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
        //根据接收到的 信息返回 相应的回答
        if (line.equals("name")) {
            bw.write("我是 nova");
        } else if (line.equals("hobby")) {
            bw.write("编写java程序");
        } else {
            bw.write("你说啥呢");
        }
//        socket.shutdownOutput();
        bw.newLine();
        bw.flush();
//        socket.shutdownOutput();//写入结束标记
        //关闭流
        bw.close();
        br.close();
        socket.close();
    }
}
